package IoLianxi;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;


public class PropertiesUtil {

    /**
     * properties输出工具
     *
     * @param path       保存路径
     * @param properties properties对象
     * @param comments   注释
     */
    public static void proFileOutput(String path, Properties properties, String comments) {

        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(path);
            properties.store(fos, comments);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    /**
     * Properties 提取工具
     *
     * @param path 文件路径
     * @return 返回 properties对象
     */
    public static Properties propertiesInput(String path) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(path);
            Properties properties = new Properties();
            properties.load(fis);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        return null;
    }
    // 将下面字符串?后面的键值对保存到proerties文件中
//    String url = "http://www.lovo.com/index?name=aaaaaa&age=20&gender=男";

    public static void main(String[] args) {
        String url = "http://www.lovo.com/index?name=aaaaaa&age=20&gender=男";
        Properties properties = new Properties();
        String str = url.substring(url.indexOf('?') + 1, url.length()); //从？开始，拿到后面的字符串
        String[] arr = str.split("&"); //把&分割，成字符串数组
        for (int i = 0; i < arr.length; i++) { //循环遍历分割
            String[] tmparr = arr[i].split("="); //分割=，得到剩下的字符放进数组中
            properties.put(tmparr[0],tmparr[1]); //放入键值对
        }
        //保存
        PropertiesUtil.proFileOutput("1.properties",properties,"2022,7,20作业");

    }
}